package cn.edu.zjut.medlinebackend.controller;

import cn.edu.zjut.medlinebackend.mapper.MembersMapper;
import cn.edu.zjut.medlinebackend.model.Members;
import cn.edu.zjut.medlinebackend.model.User;
import cn.edu.zjut.medlinebackend.service.GroupService;
import cn.edu.zjut.medlinebackend.model.Group;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/groups")
public class GroupController {
    @Resource
    private GroupService groupService;
    @Resource
    private MembersMapper membersMapper;

    @GetMapping("/createGroup")
    public boolean checkGroupCreated(String groupname,String username){
        Group group=new Group();
        group.setGroupname(groupname);
        group.setCreator(username);
        System.out.println(groupname);
        System.out.println(12312);
        if(groupService.creatGroup(group)){
            Members members = new Members();
            members.setUsername(username);
            members.setGroupname(groupname);
            membersMapper.insert(members);
            return true;
        }
        else return false;
    }
    @GetMapping("/getGroup")
    public List<Group> getGroup(String username) {
       List<Group> groups= groupService.getAllGroup(username);
        System.out.println(groups);
       return groups;
    }
    @RequestMapping("/searchGroup/{groupname}")
    public List<Group> searchGroup(@PathVariable String groupname){
       List<Group> groups= groupService.searchGroup(groupname);
        return groups;
    }
    @GetMapping("/deleteGroup")
    public boolean deleteGroup (String groupname,String username){
        return  groupService.deleteGroup(groupname,username);
    }
    @GetMapping("/exitGroup")
    public boolean exitGroup (String groupname,String username){
        return  groupService.exit(username,groupname);
    }
    @GetMapping("/joinGroup")
    public Boolean joinGroup(String groupname,String username){
        if(groupService.joinGroup(username,groupname)) return true;
        else  return false;
    }
}
